<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Client
 *
 * @author Colin
 */
class Client extends CI_Model {
    public $id;
    public $nom="";
    public $pernom="";
    public $mail="";
    public $adresse="";
    public $password="";
    private $salt = "T65Yf@sd,f";
    public function __construct()
    {

        // Call the Model constructor
        parent::__construct();
    }
    
    public function existMail($mail)
    {
        $sql="select mail from client where mail=".$this->db->escape($mail).";" ;
        $query = $this->db->query($sql);        
        return ($query->num_rows() > 0)?true:false;
    }
    
    public function save($nom, $prenom,$adresse, $mail, $password)
    {
           
            $password_chiffre = sha1(sha1($password).$this->salt);
            $sql = "INSERT INTO client (nom, prenom, adresse, mail, password) VALUES(".$this->db->escape($nom).",".$this->db->escape($prenom).",".$this->db->escape($adresse).",".$this->db->escape($mail).",".$this->db->escape($password_chiffre).")";
            try {
                $query = $this->db->query($sql);
                $clientConnecte= $this->connexion($mail,$password);
                return $clientConnecte;
            } catch (Exception $e) {
                return false;
            }
    }
    
    public function connexion($mail, $password)
    {
        $password_chiffre = sha1(sha1($password).$this->salt);
        $sql="select * from client where mail=".$this->db->escape($mail)." and password=".$this->db->escape($password_chiffre).";" ;
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0)
        {
            return $query->first_row("Client");
        }else{
           return null;
        }
    }
    
    public function findClient($id)
    {
        $sql="select * from client where id=".$this->db->escape($id).";" ;
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0)
        {
            return $query->first_row("Client");
        }else{
           return null;
        }
    }
    
}

?>
